package com.hp.dao;

import com.hp.entity.Registry;
import com.hp.entity.dto.UserDto;
import com.hp.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class RegistryDao {
    public boolean addRegistry(Registry registry) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="INSERT INTO registry(id,patient_id ,schedule_id,user_id,department_id,room_id,status,fee," +
                "pay_fee,medicare_fee,is_medicare,registry_time,created_time,updated_time) " +
                "VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
           return queryRunner.update(sql,
                   registry.getPatientId(),
                   registry.getScheduleId(),
                   registry.getUserId(),
                   registry.getDepartmentId(),
                   registry.getRoomId(),
                   registry.getStatus(),
                   registry.getFee(),
                   registry.getPayFee(),
                   registry.getMedicareFee(),
                   registry.getIsMedicare(),
                   registry.getRegistryTime(),
                   registry.getCreatedTime(),
                   registry.getUpdatedTime()
           )>0?true:false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Registry> getRegistryListByPage(Map<String, Object> map) {
        /* SELECT r.id,r.patient_id as patientId,r.schedule_id as scheduleId,r.user_id as userId,r.department_id as departmentId,r.room_id as roomId,r.status,r.fee,
                r.pay_fee as payFee,r.medicare_fee as medicareFee,r.is_medicare as isMedicare,r.registry_time as registryTime,r.created_time as createdTime,r.updated_time as updatedTime
        FROM registry AS r
        LEFT JOIN patient AS p
        ON p.id=r.`patient_id`
        WHERE p.`id_card`='140424199008293860'
        AND r.`department_id`=2
        AND r.`user_id`=68
        AND r.`status`=1*/

        StringBuilder sb=new StringBuilder();
        sb.append("SELECT r.id,r.patient_id as patientId,r.schedule_id as scheduleId,r.user_id as userId,r.department_id as departmentId,r.room_id as roomId,r.status,r.fee,");
        sb.append("r.pay_fee as payFee,r.medicare_fee as medicareFee,r.is_medicare as isMedicare,r.registry_time as registryTime,r.consultation_time AS consultationTime,r.created_time as createdTime,r.updated_time as updatedTime ");
        sb.append(" FROM registry AS r");
        sb.append(" LEFT JOIN patient AS p");
        sb.append(" ON p.id=r.patient_id");
        sb.append(" WHERE 1=1 ");
        if(map.get("idCard")!=null&&map.get("idCard").toString().length()>0){
            sb.append(" AND p.id_card='"+map.get("idCard")+"'");
        }
        if(map.get("departmentId")!=null&&map.get("departmentId").toString().length()>0){
            sb.append(" AND r.department_id="+map.get("departmentId"));
        }
        if(map.get("userId")!=null&&map.get("userId").toString().length()>0){
            sb.append(" AND r.user_id="+map.get("userId"));
        }
        if(map.get("status")!=null&&map.get("status").toString().length()>0){
            sb.append(" AND r.status="+map.get("status"));
        }
        sb.append(" order by r.id desc limit ?,?");
        String sql=sb.toString();
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        int currentPage = Integer.parseInt(map.get("currentPage").toString())-1;
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        try {
            return queryRunner.query(sql,new BeanListHandler<Registry>(Registry.class),
                    currentPage*pageSize,pageSize);
        }  catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Long totalRegistry(Map<String, Object> map) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        StringBuilder sb=new StringBuilder();
        sb.append("SELECT count(*)");
        sb.append(" FROM registry AS r");
        sb.append(" LEFT JOIN patient AS p");
        sb.append(" ON p.id=r.patient_id");
        sb.append(" WHERE 1=1 ");
        if(map.get("idCard")!=null&&map.get("idCard").toString().length()>0){
            sb.append(" AND p.id_card='"+map.get("idCard")+"'");
        }
        if(map.get("departmentId")!=null&&map.get("departmentId").toString().length()>0){
            sb.append(" AND r.department_id="+map.get("departmentId"));
        }
        if(map.get("userId")!=null&&map.get("userId").toString().length()>0){
            sb.append(" AND r.user_id="+map.get("userId"));
        }
        if(map.get("status")!=null&&map.get("status").toString().length()>0){
            sb.append(" AND r.status="+map.get("status"));
        }
        String sql=sb.toString();
        try {
            return (Long)queryRunner.query(sql, new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //更新医嘱状态
    public boolean updateRegistryStatus(Registry registry) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="update registry set STATUS=?,updated_time=?,consultation_time=? where id=?";
        try {
            return queryRunner.update(
                    sql,
                    registry.getStatus(),
                    registry.getUpdatedTime(),
                    registry.getConsultationTime(),
                    registry.getId()
            )>0?true:false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
